import {
  CHECK_ALL,
  DEL_CHECKED,
} from "./constands";
import { StateAll } from "../../../redux/state";
const initState = StateAll;
// const initState = {
//   list: [
//     {
//       todoName: "吃饭",
//       isDone: true,
//       id: 1,
//     },
//     {
//       todoName: "睡觉",
//       isDone: false,
//       id: 2,
//     },
//     {
//       todoName: "干活",
//       isDone: true,
//       id: 3,
//     },
//   ],
//   // list:JSON.parse(localStorage.getItem('key'))||[]
// };

export default function reducer(state = initState, action) {
  let newState,id;

  switch (action.type) {
    // 全选
    case CHECK_ALL:
      const checked = action.checked;

      newState = state.list.map((item) => {
        item.isDone = checked;
        return { ...item };
      });
      return {
        ...state,
        list: newState,
      };
    //  删除已选
    case DEL_CHECKED:
      newState = state.list.filter((item) => {
        return !item.isDone;
      });
      return {
        ...state,
        list: newState,
      };

    default:
      return state;
  }
}
